package com.example.springbootdemo.demos.web.controller;

import com.example.springbootdemo.demos.web.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
//Spring传递请求的参数
@RequestMapping("/param")//整体写一个路径，企业的建议，给每个controller写一个路径，
// 因为每个controller里面的方法可能会重复，所以加上一个类路径，再加上方法路径，这样重复的概率就会小一些，也方便查找代码
@RestController
public class ParameterController {
    @RequestMapping("/m1")
    public String m1(String name){
        return "接收到的参数name:" + name;
    }
    @RequestMapping("/m2")
    public String m2(String name,Integer age){
        return "接收的参数name:" + name + "age:" + age;
    }
    @RequestMapping("/m3")
    public String m3(Person person){
        return "接收到的参数：" + person.toString();
    }
    //后端参数的重命名
    @RequestMapping("/m4")
    //将前端的名称name改成了后端的username,此时name为必传的参数
    public String m4(@RequestParam("name") String username){
        return "接收到的参数name:" + username;
    }
    //将@RequestParam()里面的参数改成非必传
    @RequestMapping("/m5")
    public String m5(@RequestParam(value = "name",required = false) String username){
        return "接收到的参数name:" + username;
    }

    @RequestMapping("/m6")
    public String m6(String[] arrayParam){
        return "接收到的参数arrayParam" + Arrays.toString(arrayParam) + "数组长度为：" + arrayParam.length;
    }

//    @RequestMapping("/m7")
//    public String m7(List<String> listParam){//此时会报错
//        return "接收到的参数listParam" + listParam + "数组长度为：" + listParam.size();
//    }
    //传递集合
    @RequestMapping("/m8")
    public String m8(@RequestParam(required = false) List<String> listParam){
        return "接收到的参数listParam" + listParam + "数组长度为：" + listParam.size();
    }
    //传递json字符串
    @RequestMapping("/m9")
    public String m9(@RequestBody Person person){
        return "接收到的参数person" + person.toString();
    }
    //获取URL中的参数:比如获取
    // 到url:"https://www.toutiao.com/article/7403305406912643647/?log_from=eac5e3f2a1f0d_1724082016050"中的
    // 文章编号7403305406912643647
    @RequestMapping("/m10/{userId}")
    public String m10(@PathVariable Integer userId){
        return "userId:" + userId;
    }
    //也可以拿到URL里面的多个参数
    @RequestMapping("/m11/{userId}/{name}")
    public String m11(@PathVariable Integer userId,@PathVariable String name){
        return "userId:" + userId + ",name:" + name;
    }
    //上传文件
    @RequestMapping("/m12")
    public String m12(@RequestPart MultipartFile file) throws IOException {//用MultipartFile来接收文件，文件相关的注解都带part
        System.out.println(file.getOriginalFilename());//打印文件名字
        file.transferTo(new File("D:/picture/" + file.getOriginalFilename()));//将文件保存在D:/picture/下面
        return "success";
    }
    //cookie的获取:Servlet的方式
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie: cookies) {
//            System.out.println(cookie.getName() + ":" + cookie.getValue());
//        }
        //下面是for循环的lambda表达式写法
        if (cookies != null){
            Arrays.stream(cookies).forEach(cookie -> {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            });
        }
        return "获取cookie成功";
    }
    //Spring获取cookie的方法
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String aaa,@CookieValue String lcc){
        return "cookie存储的值aaa:" + aaa +" lcc:" + lcc;
    }
    //添加Session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();//没有session就会进行创建
        session.setAttribute("username","zhangsan");
        return "success";
    }
    //获取session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);//没有session也不会创建
        if (session!= null){
            String username = (String) session.getAttribute("username");
            return "登录用户为：" + username;
        }
        return "session为空";
    }
    //Spring获取session的方式
    @RequestMapping("/getSession1")
    public String getSession1(@SessionAttribute(required = false) String username){
        //和cookie一样，通过Spring的注解方式只能拿到session中的一个内容
        return "username" + username;
    }
    @RequestMapping("/getSession2")
    //使用Spring的内置对象,来获取session，和通过HttpServletRequest对象来获取session的方式类似
    public String getSession2(HttpSession session){//此处的HttpSession session等同于HttpSession session = request.getSession(true)
        String username = (String) session.getAttribute("username");
        return "登录用户" + username;
    }
    //获取Header，原来的Servlet的获取方式
    @RequestMapping("getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "User-Agent:" + userAgent;
    }
    ////获取Header，通过Spring注解的获取方式
    @RequestMapping("getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "User-Agent:" + userAgent;
    }
}

